﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web;

namespace KLS.PPDS.Utility.Html
{
    public static class LinkExtensions
    {
        public static MvcHtmlString DivActionLink(this HtmlHelper htmlHelper, string linkText, string actionName)
        {
            return htmlHelper.DivActionLink(linkText, actionName, null, new RouteValueDictionary(), new RouteValueDictionary());
        }

        public static MvcHtmlString DivActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object htmlAttributes)
        {
            return htmlHelper.DivActionLink(    linkText,  actionName,  null, htmlAttributes);
        }
       
        public static MvcHtmlString DivActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object htmlAttributes)
        {
            return htmlHelper.DivActionLink(linkText, actionName, controllerName, new RouteValueDictionary(), htmlAttributes);
        }

        public static MvcHtmlString DivActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            //if (string.IsNullOrEmpty(linkText))
            //{
            //    throw new ArgumentException("linkText");
            //}
            return MvcHtmlString.Create(GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, null, actionName, controllerName, new RouteValueDictionary(routeValues), new RouteValueDictionary(htmlAttributes)));
        }

        public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return GenerateLink(requestContext, routeCollection, linkText, routeName, actionName, controllerName, null, null, null, routeValues, htmlAttributes);
        }

        public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return GenerateLinkInternal(requestContext, routeCollection, linkText, routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, htmlAttributes, true);
        }

        private static string GenerateLinkInternal(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool includeImplicitMvcValues)
        {
            string str = UrlHelper.GenerateUrl(routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, routeCollection, requestContext, includeImplicitMvcValues);
            TagBuilder builder2 = new TagBuilder("div");
            builder2.InnerHtml = !string.IsNullOrEmpty(linkText) ? HttpUtility.HtmlEncode(linkText) : string.Empty;
            TagBuilder builder = builder2;
            //htmlAttributes.Remove("href");
            builder.MergeAttributes<string, object>(htmlAttributes);
            builder.MergeAttribute("href", str);
            return builder.ToString(TagRenderMode.Normal);
        }

        //private static string GenerateLinkInternalByRole(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool includeImplicitMvcValues)
        //{
        //    string str = UrlHelper.GenerateUrl(routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, routeCollection, requestContext, includeImplicitMvcValues);
        //    TagBuilder builder2 = new TagBuilder("div");
        //    builder2.InnerHtml = !string.IsNullOrEmpty(linkText) ? HttpUtility.HtmlEncode(linkText) : string.Empty;
        //    TagBuilder builder = builder2;
        //    builder.MergeAttributes<string, object>(htmlAttributes);
        //    builder.MergeAttribute("href", str);
        //    return builder.ToString(TagRenderMode.Normal);
        //}
    }
}
